package com.jdz.service.impl;

import com.jdz.common.ServerResponse;
import com.jdz.dao.GroupPrivilegeMapper;
import com.jdz.dao.PrivilegeMapper;
import com.jdz.pojo.GroupPrivilege;
import com.jdz.pojo.Privilege;
import com.jdz.service.IPrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Transactional
@Service("IPrivilegeService")
public class PrivilegeServiceimpl implements IPrivilegeService {

    @Autowired
    PrivilegeMapper privilegeMapper;
    @Autowired
    GroupPrivilegeMapper groupPrivilegeMapper;

    /**
     * 列出所有权限
     * @return
     */
    public ServerResponse listAll(){
        List<Privilege> privilegeList = privilegeMapper.selectAll();
        List<Privilege> privilegeListResult = new ArrayList<>();
        for(Privilege privilegeItem : privilegeList){
            Privilege privilege = new Privilege();
            privilege.setPermno(privilegeItem.getPermno());
            privilege.setPermname(privilegeItem.getPermname());
            privilegeListResult.add(privilege);
        }
        return ServerResponse.createBySuccess("所有权限",privilegeListResult);
    }

    /**
     * 列出本分组拥有的权限的id
     * @param groupno
     * @return
     */
    public ServerResponse list(Integer groupno){
        List<GroupPrivilege> groupPrivilegeList = groupPrivilegeMapper.selectByGroup(groupno);
        List<Integer> liat = new ArrayList<>();
        for(GroupPrivilege groupPrivilegeItem : groupPrivilegeList){
            liat.add(groupPrivilegeItem.getPermno());
        }
        return ServerResponse.createBySuccess("列出本分组拥有的权限的id",liat);
    }

    /**
     * 删除权限分组关联
     * @param permno
     * @param groupno
     * @return
     */
    public ServerResponse deleteAnd(Integer permno,Integer groupno){
        if(permno==0||permno==null||groupno==0||groupno==null){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        if(groupno==1){
            return ServerResponse.createByErrorMessage("不能修改用户分组权限");
        }
        if(groupno==3){
            return ServerResponse.createByErrorMessage("不能修改超级管理员分组权限");
        }

        int row = groupPrivilegeMapper.deleteBypermNo(permno,groupno);

        if(row<=0){
            return ServerResponse.createByErrorMessage("删除本分组权限失败");
        }
        return  ServerResponse.createBySuccessMessage("删除本分组权限成功");
    }

    /**
     * 添加分组权限分组关联
     * @param groupPrivilegeForm
     * @return
     */
    public ServerResponse addAnd(GroupPrivilege groupPrivilegeForm){
        if(groupPrivilegeForm==null||groupPrivilegeForm.getPermno()==null||groupPrivilegeForm.getGroupno()==null){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        if(groupPrivilegeForm.getGroupno()==1){
            return ServerResponse.createByErrorMessage("不能修改用户分组权限");
        }
        if(groupPrivilegeForm.getGroupno()==3){
            return ServerResponse.createByErrorMessage("不能修改超级管理员分组权限");
        }
        groupPrivilegeForm.setGroprivino(null);
        groupPrivilegeForm.setCreattime(new Date());

        GroupPrivilege g = groupPrivilegeMapper.selectByGroupNoPermNo(groupPrivilegeForm.getPermno(),groupPrivilegeForm.getGroupno());

        if(g != null){
            return ServerResponse.createByErrorMessage("权限已存在");
        }

        int row = groupPrivilegeMapper.insertSelective(groupPrivilegeForm);
        if(row<=0){
            return ServerResponse.createByErrorMessage("添加分组权限失败");
        }
        return  ServerResponse.createBySuccessMessage("添加本分组权限成功");
    }
}
